package cn.sxt.sys.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.sxt.sys.domain.LeaveBill;
import cn.sxt.sys.service.LeaveBillService;
import cn.sxt.sys.service.WorkFlowService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.SessionUtils;
import cn.sxt.sys.vo.WorkFlowVo;

@Controller
@RequestMapping("workFlow")
public class WorkFlowController {

	@Autowired
	private WorkFlowService workFlowService;
	
	@Autowired
	private LeaveBillService leaveBillService;

	/**
	 * 跳到流程管理页面
	 */
	@RequestMapping("toWorkFlowManager")
	public String toWorkFlowManager() {
		return "system/workFlow/workFlowManager";
	}

	/**
	 * 加载流程部署信息
	 */
	@RequestMapping("loadAllProcessDeployment")
	@ResponseBody
	public DataGridView loadAllProcessDeployment(WorkFlowVo workFlowVo) {
		return workFlowService.loadAllProcessDeployment(workFlowVo);
	}

	/**
	 * 加载流程定义信息
	 */
	@RequestMapping("loadAllProcessDef")
	@ResponseBody
	public DataGridView loadAllProcessDef(WorkFlowVo workFlowVo) {
		return workFlowService.loadAllProcessDef(workFlowVo);
	}

	/**
	 * 跳到部署流程页面
	 */
	@RequestMapping("toAddDeployment")
	public String toAddDeployment(WorkFlowVo workFlowVo) {
		return "system/workFlow/workFlowAdd";
	}

	/**
	 * 上传，部署
	 */
	@RequestMapping("addDeployment")
	@ResponseBody
	public Map<String, Object> addDeployment(MultipartFile mf, WorkFlowVo workFlowVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "部署成功";
		try {
			workFlowService.addDeployment(mf.getInputStream(), workFlowVo.getDeploymentName());
		} catch (Exception e) {
			e.printStackTrace();
			msg = "部署失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteDeployment")
	@ResponseBody
	public Map<String, Object> deleteDeployment(WorkFlowVo workFlowVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = workFlowService.deleteDeployment(workFlowVo.getDeploymentId());
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	/**
	 * 跳到查看流程图页面
	 */
	@RequestMapping("toViewProcessImage")
	public String toViewProcessImage(WorkFlowVo workFlowVo) {
		return "system/workFlow/viewProcessImage";
	}

	/**
	 * 查看流程图
	 */
	@RequestMapping("viewProcessImage")
	public void viewProcessImage(WorkFlowVo workFlowVo, HttpServletResponse response) {
		// 得到流程图
		InputStream is = workFlowService.viewProcessImage(workFlowVo.getDeploymentId());
		try {
			// 把流程图转成图片
			BufferedImage image = ImageIO.read(is);
			// 把图片流写出
			ImageIO.write(image, "JPEG", response.getOutputStream());
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 提交申请
	 */
	@RequestMapping("startLeaveBillProcess")
	@ResponseBody
	public Map<String, Object> startLeaveBillProcess(WorkFlowVo workFlowVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "启动成功";
		try {
			workFlowService.startLeaveBillProcess(workFlowVo.getId());
		} catch (Exception e) {
			e.printStackTrace();
			msg = "启动失败";
		}
		map.put("msg", msg);
		return map;
	}

	/**
	 * 跳到我的待办任务 页面
	 */
	@RequestMapping("toTaskManager")
	public String toTaskManager(WorkFlowVo workFlowVo) {
		return "system/workFlow/taskManager";
	}
	
	/**
	 * 查当前用户的待办任务
	 */
	@RequestMapping("queryCurrentUserTask")
	@ResponseBody
	public DataGridView queryCurrentUserTask(WorkFlowVo workFlowVo) {
		return workFlowService.queryCurrentUserTask(workFlowVo);
	} 
	
	/**
	 * 跳到任务办理 页面
	 */
	@RequestMapping("toDoTask")
	public String toDoTask(WorkFlowVo workFlowVo,Model model) {
		//1,根据任务id查请假单信息
		LeaveBill leaveBill=workFlowService.queryLeaveBillByTaskId(workFlowVo.getTaskId());
		//2,根据任务id查按钮的集合
		List<String> outcomes=workFlowService.queryOutcomeByTaskId(workFlowVo.getTaskId());
		model.addAttribute("leaveBill", leaveBill);
		model.addAttribute("outcomes", outcomes);
		return "system/workFlow/doTaskManager";
	}
	
	/**
	 * 根据任务id查 批注信息
	 */
	@RequestMapping("queryCommonsByTaskId")
	@ResponseBody
	public DataGridView queryCommonsByTaskId(WorkFlowVo workFlowVo) {
		return workFlowService.queryCommonsByTaskId(workFlowVo.getTaskId());
	}
	
	/**
	 * 完成任务
	 */
	@RequestMapping("completeTask")
	@ResponseBody
	public Map<String, Object> completeTask(WorkFlowVo workFlowVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "完成任务成功";
		try {
			workFlowService.completeTask(workFlowVo);
		} catch (Exception e) {
			e.printStackTrace();
			msg = "完成任务失败";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 根据任务id查任务办理的流程图
	 */
	@RequestMapping("toViewProcessImageByTaskId")
	public String toViewProcessImageByTaskId(WorkFlowVo workFlowVo,Model model) {
		//1，根据任务id查流程定义对象
		ProcessDefinition processDefinition=workFlowService.queryProcessDefinitionByTaskId(workFlowVo.getTaskId());
		//2,取出流程部署
		String deploymentId = processDefinition.getDeploymentId();
		workFlowVo.setDeploymentId(deploymentId);
		//3，根据任务id查当前办理任务的坐标
		Map<String,Object> coordinate=workFlowService.queryCoordinateByTaskId(workFlowVo.getTaskId());
		model.addAttribute("c", coordinate);
		return "system/workFlow/viewProcessImage";
	}
	
	/**
	 * 跳到 审批查询 的页面
	 */
	@RequestMapping("toViewApprovalProgress")
	public String toViewApprovalProgress(WorkFlowVo workFlowVo,Model model) {
		//请假单的信息
		LeaveBill leaveBill = leaveBillService.queryLeaveBillById(workFlowVo.getId());
		model.addAttribute("leaveBill", leaveBill);
		return "system/workFlow/showApprovalProgress";
	}
	
	/**
	 * 根据请假单id查批注
	 */
	@RequestMapping("queryCommonsByLeaveBillId")
	@ResponseBody
	public DataGridView queryCommonsByLeaveBillId(WorkFlowVo workFlowVo) {
		return workFlowService.queryCommonsByLeaveBillId(workFlowVo.getId());
	}
	
	/**
	 * 跳到 历史审批记录 的页面
	 */
	@RequestMapping("toHistoryApprovalProgress")
	public String toHistoryApprovalProgress(WorkFlowVo workFlowVo) {
		return "system/workFlow/historyApprovalProgress";
	}
	
	/**
	 * 根据当前用户名称查询历史审批记录
	 */
	@RequestMapping("queryHistoryTaskByAssignee")
	@ResponseBody
	public DataGridView queryHistoryTaskByAssignee(WorkFlowVo workFlowVo) {
		workFlowVo.setAssignee(SessionUtils.getUserNameInSession("user"));
		return workFlowService.queryHistoryTaskByAssignee(workFlowVo);
	}
	
}
